route.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET } from "./route.js";
  11. describe("GET /api/branches/[branch]/years", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-years-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. // Minimal structure for NL01
  19. await fs.mkdir(path.join(tmpRoot, "NL01", "2024"), { recursive: true });
  20. });
  21. afterEach(async () => {
  22. process.env.NAS_ROOT_PATH = originalNasRoot;
  23. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  24. });
  25. it("returns 401 when unauthenticated", async () => {
  26. getSession.mockResolvedValue(null);
  27. const res = await GET(
  28. new Request("http://localhost/api/branches/NL01/years"),
  29. { params: Promise.resolve({ branch: "NL01" }) }
  30. );
  31. expect(res.status).toBe(401);
  32. expect(await res.json()).toEqual({
  33. error: { message: "Unauthorized", code: "AUTH_UNAUTHENTICATED" },
  34. });
  35. });
  36. it("returns 403 when branch user accesses a different branch", async () => {
  37. getSession.mockResolvedValue({
  38. role: "branch",
  39. branchId: "NL01",
  40. userId: "u1",
  41. });
  42. const res = await GET(
  43. new Request("http://localhost/api/branches/NL02/years"),
  44. { params: Promise.resolve({ branch: "NL02" }) }
  45. );
  46. expect(res.status).toBe(403);
  47. expect(await res.json()).toEqual({
  48. error: { message: "Forbidden", code: "AUTH_FORBIDDEN_BRANCH" },
  49. });
  50. });
  51. it("returns years for a valid branch when allowed", async () => {
  52. getSession.mockResolvedValue({
  53. role: "branch",
  54. branchId: "NL01",
  55. userId: "u1",
  56. });
  57. const res = await GET(
  58. new Request("http://localhost/api/branches/NL01/years"),
  59. { params: Promise.resolve({ branch: "NL01" }) }
  60. );
  61. expect(res.status).toBe(200);
  62. expect(await res.json()).toEqual({ branch: "NL01", years: ["2024"] });
  63. });
  64. it("returns 400 when branch param is missing (authenticated)", async () => {
  65. getSession.mockResolvedValue({
  66. role: "admin",
  67. branchId: null,
  68. userId: "u2",
  69. });
  70. const res = await GET(new Request("http://localhost/api/branches//years"), {
  71. params: Promise.resolve({ branch: undefined }),
  72. });
  73. expect(res.status).toBe(400);
  74. expect(await res.json()).toEqual({
  75. error: {
  76. message: "Missing required route parameter(s)",
  77. code: "VALIDATION_MISSING_PARAM",
  78. details: { params: ["branch"] },
  79. },
  80. });
  81. });
  82. it("returns 404 when the branch folder does not exist (authorized)", async () => {
  83. getSession.mockResolvedValue({
  84. role: "admin",
  85. branchId: null,
  86. userId: "u2",
  87. });
  88. const res = await GET(
  89. new Request("http://localhost/api/branches/NL99/years"),
  90. { params: Promise.resolve({ branch: "NL99" }) }
  91. );
  92. expect(res.status).toBe(404);
  93. expect(await res.json()).toEqual({
  94. error: {
  95. message: "Not found",
  96. code: "FS_NOT_FOUND",
  97. details: { branch: "NL99" },
  98. },
  99. });
  100. });
  101. it("returns 500 when NAS_ROOT_PATH is invalid (authenticated)", async () => {
  102. getSession.mockResolvedValue({
  103. role: "admin",
  104. branchId: null,
  105. userId: "u2",
  106. });
  107. process.env.NAS_ROOT_PATH = path.join(tmpRoot, "does-not-exist");
  108. const res = await GET(
  109. new Request("http://localhost/api/branches/NL01/years"),
  110. { params: Promise.resolve({ branch: "NL01" }) }
  111. );
  112. expect(res.status).toBe(500);
  113. expect(await res.json()).toEqual({
  114. error: { message: "Internal server error", code: "FS_STORAGE_ERROR" },
  115. });
  116. });
  117. });